
Public Class UrineAbnormalStatusHistory

    Protected _year As String
    Protected _police As New Police()
    Protected _urine_abnormal As New UrineAbnormal()

    Public Property police() As Police
        Get
            Return _police
        End Get
        Set(ByVal value As Police)
            _police = value
        End Set
    End Property

    Public Property year() As String
        Get
            Return _year
        End Get
        Set(ByVal value As String)
            _year = value
        End Set
    End Property

    Public Property urine_abnormal() As UrineAbnormal
        Get
            Return _urine_abnormal
        End Get
        Set(ByVal value As UrineAbnormal)
            _urine_abnormal = value
        End Set
    End Property

    Public Sub deletePoliceUrineResultAbnormalStatus(ByVal inUrineResultHistory As UrineResultHistory)
        Try
            Dim decryptText As New DateUtilities.Encrypt_Data()
            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
                decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
                "PoliceHealthConnectionString").ToString(), True))
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
            objCmd.Connection = objConn
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.CommandText = "proc_DELETE_PoliceUrineAbnormalStatusHistory"

            Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inCitizenID", inUrineResultHistory.police.citizen_id)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inYear", inUrineResultHistory.year)
            objCmd.Parameters.Add(objParam)

            objConn.Open()
            objCmd.ExecuteNonQuery()
            objConn.Close()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Function insertPoliceUrineAbnormalStatusHistory(ByVal inUrineResultHistory As UrineResultHistory)
        Dim blResultStatus As Boolean = True
        For i As Integer = 1 To 8
            blResultStatus = blResultStatus And insertPoliceUrineAbnormalStatusHistory(inUrineResultHistory, i)
        Next
        Return blResultStatus
    End Function

    Public Function insertPoliceUrineAbnormalStatusHistory(ByVal inUrineResultHistory As UrineResultHistory, _
        ByVal inLoop As Short) As Boolean
        Try
            Dim decryptText As New DateUtilities.Encrypt_Data()
            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
                decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
                "PoliceHealthConnectionString").ToString(), True))
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
            objCmd.Connection = objConn
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.CommandText = "proc_INSERT_PoliceUrineAbnormalStatusHistory"

            Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inCitizenID", _
                Utilities.ConvertCitizenID(inUrineResultHistory.police.citizen_id))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inYear", inUrineResultHistory.year)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationID", _
                inUrineResultHistory.police.organization.organization_id)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationNameTha", _
                inUrineResultHistory.police.organization.organization_name_tha)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCreateUser", inUrineResultHistory.police.create_user)
            objCmd.Parameters.Add(objParam)

            Select Case inLoop
                Case 1
                    If inUrineResultHistory.urine_color.color_id = 5 And _
                    Not IsDBNull(Utilities.SendToDatabase(inUrineResultHistory.urine_color.color_id)) Then
                        If CInt(inUrineResultHistory.rbc.ToString.Substring(3, 2)) > 3 Then
                            objParam = New MySql.Data.MySqlClient.MySqlParameter("inUrineAbnormalID", 2)
                            objCmd.Parameters.Add(objParam)

                            objConn.Open()
                            objCmd.ExecuteNonQuery()
                            objConn.Close()
                            Return False
                        End If
                    End If
                Case 2
                    If inUrineResultHistory.ph < 4.8 And Not IsDBNull(Utilities.SendToDatabase(inUrineResultHistory.ph)) Then
                        Return False
                    End If
                Case 3
                    If inUrineResultHistory.spgr < 1.0 And Not IsDBNull(Utilities.SendToDatabase(inUrineResultHistory.spgr)) Then
                        Return False
                    End If
                Case 4
                    If inUrineResultHistory.urine_albumin.urine_albumin_id >= 3 _
                    And Not IsDBNull(Utilities.SendToDatabase(inUrineResultHistory.urine_albumin.urine_albumin_id)) Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inUrineAbnormalID", 1)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()
                        Return False
                    End If
                Case 5
                    If inUrineResultHistory.urine_sugar.urine_sugar_id >= 3 _
                    And Not IsDBNull(Utilities.SendToDatabase(inUrineResultHistory.urine_sugar.urine_sugar_id)) Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inUrineAbnormalID", 4)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()
                        Return False
                    End If
                Case 6
                    Dim objMyFunction As New DateUtilities.Utilities()
                    Dim objArray() As String = objMyFunction.spliteText(inUrineResultHistory.rbc, "-", 2)
                    If (Not IsDBNull(Utilities.SendToDatabase(inUrineResultHistory.rbc))) AndAlso _
                    CInt(objArray(1)) > 3 Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inUrineAbnormalID", 2)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()
                        Return False
                    End If
                Case 7
                    Dim objMyFunction As New DateUtilities.Utilities()
                    Dim objArray() As String = objMyFunction.spliteText(inUrineResultHistory.wbc, "-", 2)
                    If (Not IsDBNull(Utilities.SendToDatabase(inUrineResultHistory.wbc))) AndAlso _
                    CInt(objArray(1)) > 3 Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inUrineAbnormalID", 3)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()
                        Return False
                    End If
                Case 8
                    Dim objMyFunction As New DateUtilities.Utilities()
                    Dim objArray() As String = objMyFunction.spliteText(inUrineResultHistory.epithelium_cell, "-", 2)
                    If (Not IsDBNull(Utilities.SendToDatabase(inUrineResultHistory.epithelium_cell))) AndAlso _
                    CInt(objArray(1)) > 30 Then
                        Return False
                    End If
            End Select
        Catch ex As Exception
            Dim msgWarnning As New ManageUrineResultHistoryControl()
            msgWarnning.showInsertUrineAbnormalStatusHistoryError()
        End Try
        Return True
    End Function

End Class
